﻿using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SWS = System.Web.Security;

namespace Ventigrate.Shared.ExternalMembership.Internal
{
    public class ExtranetContext : IDisposable
    {
        #region Const

        public const string EXTRANET_URL = "http://extranet";

        #endregion

        #region Instance

        public static ExtranetContext Default
        {
            get
            {
                return new ExtranetContext(null);
            }
        }

        public static ExtranetContext Create(string siteUrl)
        {
            return new ExtranetContext(siteUrl);
        }

        private ExtranetContext(string siteUrl)
        {
            this._siteUrl = siteUrl;
        }

        private ExtranetContext()
        { }

        #endregion

        #region Properties

        private string _siteUrl = null;
        public String SiteUrl { get { return _siteUrl ?? EXTRANET_URL; } }


         private string _membershipProviderName = null;
        public string MembershipProviderName
        {
            get
            {
                string result = null;

                if (null == _membershipProviderName)
                {
                    if (this.MembershipProvider != null)
                        result = SWS.Membership.Provider.Name;
                }

                return result;
            }
        }

        private string _roleProviderName = null;
        public string RoleProviderName
        {
            get
            {
                string result = null;

                if (null == _roleProviderName)
                {
                    if (this.RoleProvider != null)
                        result = this.RoleProvider.Name;
                }

                return result;
            }
        }

        private SPIisSettings _iisSettings = null;
        internal SPIisSettings IisSettings
        {
            get
            {
                if (null == _iisSettings)
                {
                    using (SPSite site = new SPSite(EXTRANET_URL))
                    {
                        SPWebApplication webApp = site.WebApplication;
                        _iisSettings = webApp.IisSettings[site.Zone];
                    }
                }

                return _iisSettings;
            }
        }

        private SWS.MembershipProvider _membershipProvider = null;
        internal SWS.MembershipProvider MembershipProvider
        {
            get
            {
                if (null == _membershipProvider)
                {
                    try
                    {
                        _membershipProvider = SWS.Membership.Providers[IisSettings.FormsClaimsAuthenticationProvider.MembershipProvider];
                    }
                    catch (Exception ex)
                    {
                        ULS.Log.Error(ex);
                    }
                }

                return _membershipProvider;
            }
        }

        private SWS.RoleProvider _roleProvider = null;
        internal SWS.RoleProvider RoleProvider
        {
            get
            {
                if (null == _roleProvider)
                {
                    try
                    {
                        _roleProvider = SWS.Roles.Providers[IisSettings.FormsClaimsAuthenticationProvider.RoleProvider];
                    }
                    catch (Exception ex)
                    {
                        ULS.Log.Error(ex);
                    }
                }

                return _roleProvider;
            }
        }

        #endregion

        #region Events

        /// <summary>
        /// Disposal is not required, it is just to make use of the using() pattern to create a scope with local reference
        /// </summary>
        public void Dispose()
        { }

        #endregion
    }
}
